/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/util/AudioPlayer.fx $
******************************************************************************/

package de.hft_stuttgart.robofight.client.javafx.util;


import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

/**
 * @author ralf.dauenhauer@gmx.de
 *
 * Utility Class
 * Loads and plays *.wav files
 */
public class AudioPlayer {
    var audioMap = new java.util.HashMap();

    public function initialize(sourceIP : String){
         //sounds for the menue
        var path = "http://{sourceIP}/_res/";
        audioMap.put("menue", loadAudioClip(new URL("{path}switch.wav")));

        //sounds for weapons
        audioMap.put("Lightning", loadAudioClip(new URL("{path}lightning.wav")));
        audioMap.put("Laser", loadAudioClip(new URL("{path}laser.wav")));
        audioMap.put("Bazooka", loadAudioClip(new URL("{path}bazooka.wav")));
        audioMap.put("RailGun", loadAudioClip(new URL("{path}railgun.wav")));
        audioMap.put("Plasma", loadAudioClip(new URL("{path}plasma.wav")));
        audioMap.put("Photonen", loadAudioClip(new URL("{path}photonen.wav")));
        audioMap.put("Lightfraser", loadAudioClip(new URL("{path}lightfaser.wav")));
        audioMap.put("SolarImpuls", loadAudioClip(new URL("{path}solarImpuls.wav")));

        audioMap.put("botDied", loadAudioClip(new URL("{path}explode.wav")));
    }

  /**
   * loads a AudioClip, on any exception the AudioClip will be null and will not be played
   * @param URL
   */
    public function loadAudioClip(url : URL) : AudioClip {
        var clip : AudioClip = null;
        try {
            clip = Applet.newAudioClip(url);
            return clip;
        } catch (MediaUnavailableException){
            return null;
        } catch (MalformedURLException) {
            return null;
        }
    }

   /**
    * plays a sound
    * @param audioName - String
    */
    public function play(audioName : String){
        if(audioMap.get(audioName) != null){
            (audioMap.get(audioName) as AudioClip).play();
        }
    }

   /**
    * loops a sound
    * @param audioName - String
    */
    public function loop(audioName : String){
        if(audioMap.get(audioName) != null){
            (audioMap.get(audioName) as AudioClip).play();
        }
    }

    /**
     * mutes sound
     */
//    public function mute(){
//        this.volume = 0;
//    }
}
